// pages/register/register.js
const app = getApp()
var url = require('../../utils/url.js');
var token;
var formid;
var userinfo;
var openId;
var base64 = require('../../utils/base64.js');
var share_user;
var token;
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var scene = decodeURIComponent(options.scene)
    var index = scene.lastIndexOf("\=");
    share_user = scene.substring(index + 1, scene.length);
    console.log(share_user)
    this.setData({
      share_user: share_user
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  register: function (e) {
    var that = this;
    console.log(e)
    var formid = e.detail.formId
    console.log(this)
    if (!token){
      wx.showToast({
        title: '登录中',
        icon: 'loading',
      })
      wx.login({
        success: function (response) {
          var code = response.code
          console.log(response)
          if (response.code) {
            wx.getUserInfo({
              success: function (resp) {
                var nickName = resp.userInfo.nickName;
                var avatarUrl = resp.userInfo.avatarUrl;
                wx.request({
                  url: url.URl + 'login/mini',
                  data: { code: code },
                  method: 'POST',
                  success: function (res) {
                    console.log(res.data)
                    userinfo = resp.userInfo;
                    var jsonopenId;
                    var basepenId
                    openId = res.data.data.openid;

                    let data = {};
                    var openID = "openid";
                    let from = "from"
                    var type = "type";
                    var mini_formid = "mini_formid";
                    var nickName1 = "nickName";
                    var avatarUrl1 = "avatar";
                    var shareUser = "share_user";
                    data[openID] = openId;
                    data[nickName1] = nickName;
                    data[type] = "share";
                    data[from] = "mini";
                    data[avatarUrl1] = avatarUrl;
                    data[mini_formid] = formid;
                    data[shareUser] = share_user;
                    console.log(data)
                    jsonopenId = JSON.stringify(data)
                    // console.log(jsonopenId)
                    basepenId = base64.Base64.encode(jsonopenId)
                    console.log(basepenId)
                    var session = res.data.data.session_key;
                    var enBasepenId = encodeURIComponent(basepenId)
                    console.log(enBasepenId)
                    wx.request({
                      url: url.URl + 'login',
                      data: { ihy: enBasepenId },
                      method: 'POST',
                      success: function (res) {
                        var msg = res.data.data
                        console.log(res)
                        if (res.data.code == 200) {
                          // console.log(res)

                          var userinfo = res.data.data.userInfo;
                          token = res.data.data.token;
                          that.setData({
                            userinfo: userinfo
                          })
                          wx.setStorage({
                            key: 'userinfo',
                            data: userinfo,
                          })
                          wx.setStorage({
                            key: 'token',
                            data: token,
                          })
                          wx.hideToast()
                          wx.showToast({
                            title: '登录成功',
                            icon: 'success',
                            duration: 2000,
                            success: function () {
                              setTimeout(() => {
                                // 模拟请求数据，并渲染
                                wx.switchTab({
                                  url: '/pages/index/index',
                                  success: function (res) { },
                                  fail: function (res) { },
                                  complete: function (res) { },
                                })
                                // 数据成功后，停止下拉刷新
                              }, 1000);
                            }
                          })
                        } else {
                          wx.showToast({
                            title: msg,
                            icon: 'success',
                            duration: 2000,
                            success: function () {
                            }
                          })
                        }
                      }
                    })
                  }
                })

              }, fail: function () {
                console.log("用户拒绝授权")
                wx.hideLoading()
              }
            })
          } else {
            console.log('获取用户登录态失败！' + response.errMsg)
          }
        }
      });
    }else{
       wx.showModal({
         title: '提示',
         content: '您已经登录过了',
         showCancel: false,
         confirmText: '我知道了',
         confirmColor: '#DD2626',
         success: function(res) {
           wx.switchTab({
             url: '/pages/index/index',
             success: function(res) {},
             fail: function(res) {},
             complete: function(res) {},
           })
         },
         fail: function(res) {},
         complete: function(res) {},
       })
    }
    
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
     wx.getStorage({
       key: 'token',
       success: function(res) {
         token=res.data
       },
     })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})